package cn.wxManage.goodsManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.wxManage.common.dao.ImageInfoMapper;
import cn.wxManage.goodsManage.dao.GoodsMapper;
import cn.wxManage.goodsManage.dao.GoodsStyleMapper;
import cn.wxManage.goodsManage.model.Goods;
import cn.wxManage.goodsManage.model.GoodsStyle;
import cn.wxManage.goodsManage.service.IGoodsStyleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Zhang on 2018/11/27.
 */
@Service
public class GoodsStyleStyleManageService implements IGoodsStyleManageService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsStyleMapper goodsStyleMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    ImageInfoMapper imageInfoMapper;

    @Override
    public ServerResponse<?> getGoodsStyleByCode(String code) {
        GoodsStyle goodsStyle = goodsStyleMapper.getInfoByCode(code);
        if (goodsStyle == null){
            return ServerResponse.createByErrorMessage("该品种不存在");
        }
        Goods goods = goodsMapper.getGoodsInfo(goodsStyle.getGoodsId());
        if (goods == null){
            return ServerResponse.createByErrorMessage("该商品不存在");
        }
        goods.setGoodsStyle(goodsStyle);
        return ServerResponse.createBySuccess(goods);
    }

    @Override
    public ServerResponse<?> updateGoodsStyleBalance(GoodsStyle goodsStyle) {
        if ("send".equals(goodsStyle.getType())){
            goodsStyle.setBalance(goodsStyle.getBalance() * -1);
        }
        goodsStyleMapper.updateGoodsStyleBalance(goodsStyle);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> getGoodsStyleById(Integer goodsId) {
        List<GoodsStyle> styleList = goodsStyleMapper.getGoodsStyleByGoodsId(goodsId);
        return ServerResponse.createBySuccess(styleList);
    }

    @Override
    public ServerResponse<?> getGoodsByStyleId(Integer goodsStyleId) {
        GoodsStyle goodsStyle = goodsStyleMapper.getInfoById(goodsStyleId);
        Goods goods = goodsMapper.getGoodsInfo(goodsStyle.getGoodsId());
        goods.setGoodsStyle(goodsStyle);
        goods.setMainImgPath(imageInfoMapper.getGoodsMain(goods.getId()));
        return ServerResponse.createBySuccess(goods);
    }
}
